﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;
namespace BLL
{
    public class LocalDoctorBLL
    {
        HospitalDataContext DB = new HospitalDataContext();
        private static int localdoctorid = 0;
        public List<LocalDoctor> SelectAll()
        {
            return DB.LocalDoctors.ToList();
        }

        public string Insert(LocalDoctorDTO nhap)
        {
            try
            {
                LocalDoctor localdoctor = new LocalDoctor();
                localdoctor.LocalDoctorID = (localdoctorid++).ToString();
                localdoctor.FullName = nhap.Fullname;
                localdoctor.ClinicNo = nhap.Clinicno;
                localdoctor.Address = nhap.Address;
                localdoctor.Tel = nhap.Tel;

                DB.LocalDoctors.InsertOnSubmit(localdoctor);
                DB.SubmitChanges();
                return "success";
            }
            catch(Exception e) {
               return e.Message;
            }
        }
        //--------------ham update-----------
        public string Update(LocalDoctorDTO nhap)
        {
            try
            {
                LocalDoctor localdoctor = DB.LocalDoctors.Where(ld=>ld.LocalDoctorID==nhap.Localdoctorid).FirstOrDefault();
                localdoctor.FullName = nhap.Fullname;
                localdoctor.ClinicNo = nhap.Clinicno;
                localdoctor.Address = nhap.Address;
                localdoctor.Tel = nhap.Tel;

                DB.SubmitChanges();
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        //get ID
        public string GetID(LocalDoctorDTO nhap) 
        {
        //    var id = from ld in DB.LocalDoctors
        //                        where ld.FullName == nhap.Fullname && ld.ClinicNo == nhap.Clinicno && ld.Address == nhap.Address && ld.Tel == nhap.Tel
        //                        select ld;
        //    return id.LocalDoctorID;
            string id = DB.LocalDoctors.Where(ld => ld.FullName == nhap.Fullname && ld.ClinicNo == nhap.Clinicno && ld.Address==nhap.Address && ld.Tel == nhap.Tel).Select(n=>n.LocalDoctorID).FirstOrDefault().ToString();
            return id;
        }
        public List<LocalDoctorAndPatientDTO> SearchWithPatientID(string key)
        { 
            return DB.LocalDoctors.Join(
                DB.Patients,
                a=>a.LocalDoctorID,
                b=>b.LocalDoctorID,
                (a, b) => new LocalDoctorAndPatientDTO
                {
                    LocalDoctorID=a.LocalDoctorID,
                    FullName=a.FullName,
                    ClinicNo=a.ClinicNo,
                    Address=a.Address,
                    Tel=a.Tel,
                    PatientID=b.PatientID
                }
                ).Where(ld => ld.PatientID.Contains(key)).ToList < LocalDoctorAndPatientDTO>();
        }
    }
}
